#include "uart_handle.h"

static u8 g_uart_buf[1024];
char buf_device[17]="2000000000000001";
static void print_data_hex(uint8_t *data, int len)
{   
    for (int i = 0; i < len; i++) 
    {
        printf(" %02x", data[i]);
    }
    printf(", len1: %d\n", len);
}

void UartPortSerialRxPoll(uart_port_t uart_num,size_t xEventSize)
{
    size_t buffered_size;
    uint8_t dtmp[UART_BUFFER_SIZE];
    cache_param *cache_uart_buffer=NULL;
    u8 *data;
    uart_get_buffered_data_len(uart_num,&buffered_size);    //读取RX缓冲区里的数据长度
    uart_read_bytes(uart_num,dtmp,buffered_size,portMAX_DELAY);       //这里的数据长度就不能按照例程里的使用event.size，因为这个值最大是120 也就是FIFO的最大接收长
    //print_data_hex(dtmp,buffered_size);
    printf("dtmp[0]%d,size %d\n",dtmp[0],buffered_size);
    cache_uart_buffer = pvPortMalloc(sizeof(cache_param *)); //创建结构体缓冲区
    cache_uart_buffer->len = buffered_size;
    cache_uart_buffer->source = UART_SOURCE;
    data = (u8 *)pvPortMalloc(buffered_size*sizeof(u8 *));
    memcpy(data,dtmp,buffered_size);
    cache_uart_buffer->data =data;

    if (pdPASS != xQueueSend(cache_queue_handle,&cache_uart_buffer,0))
    {
        LOG_INFO("xQueueSend fail\n");
        vPortFree(cache_uart_buffer->data);
        vPortFree(cache_uart_buffer);
    }

}


void init_uart_handle()
{
    printf("into app_main\n");
    set_uart1_handle_func(&UartPortSerialRxPoll);
    uart1_config(UART1_TX_PIN,UART1_RX_PIN,UART_RATE, false);
    uart1_init(); //串口任务初始化
}